package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * Created by lenovo on 2017/10/31.
 */
public class LoadLabMasterListTransfer extends AbstractTransfer{
    public LoadLabMasterListTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String currentPage = "1";
        String endNum ="dataRows.size()";
        String hasNextPage ="false";
        String hasPreviousPage ="true";
        String cardNo = (String) params.get("cardNo");
        Calendar calendar = Calendar.getInstance();
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("DataNode1").addElement("DataRow");
        dataRowElement.addAttribute("START_DATE", startDate).addAttribute("END_DATE", endDate).addAttribute("ID_NO", cardNo);
        return tplDoc.asXML();
    }
    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (dataRows != null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String, Object> dataMap = new HashMap<>();
                    LoadLabMasterListTransfer.LoadLabMasterListInnerMessage dataRowMessage = new LoadLabMasterListTransfer.LoadLabMasterListInnerMessage(dataRow);
                    dataMap.put("repId", dataRowMessage.getRepId());
                    dataMap.put("repTime", dataRowMessage.getRepTime());
                    dataMap.put("deptName", dataRowMessage.getDeptName());
                    dataMap.put("docName", dataRowMessage.getDocName());
                    dataMap.put("itemNo", dataRowMessage.getItemNo());
                    dataMap.put("repName", dataRowMessage.getRepName());
                    dataMap.put("specimen", dataRowMessage.getSpecimen());
                    dataMap.put("regTime", dataRowMessage.getRegTime());
                    dataList.add(dataMap);
                }
            }
                resMap.put("list",dataList);
                    resMap.put("code", executeStatus);
                    resMap.put("message", executeMessage);
                    return resMap;
                } else {
                    resMap.put("code", executeStatus);
                    resMap.put("message", executeMessage);


                }
        return resMap;
            }

    private class LoadLabMasterListInnerMessage implements InnerMessage {
        /**
         * 报告单号
         */
        private String repId;
        /**
         * 报告时间
         */
        private String repTime;
        /**
         * 申请科室名称
         */
        private String deptName;
        /**
         * 申请医生名称
         */
        private String docName;
        /**
         * 项目编号
         */
        private String itemNo;
        /**
         * 报告名称
         */
        private String repName;
        /**
         * 化验标本
         */
        private String specimen;
        /**
         * 申请时间
         */
        private String regTime;

        public LoadLabMasterListInnerMessage(Element element){
            this.repId = element.attributeValue("TEST_NO");
            this.repTime = element.attributeValue("RESULTS_RPT_DATE_TIME");
            this.deptName = element.attributeValue("DEPT_NAME");
            this.docName = element.attributeValue("ORDERING_PROVIDER");
            this.itemNo = element.attributeValue("ITEM_CODE");
            this.repName = element.attributeValue("ITEM_NAME");
            this.specimen = element.attributeValue("SPECIMEN");
            this.regTime = element.attributeValue("REQUESTED_DATE_TIME");
        }
        public String getRepId() { return repId; }

        public String getRepTime() { return repTime; }

        public String getDeptName() { return deptName; }

        public String getDocName() { return docName; }

        public String getItemNo() { return itemNo; }

        public String getRepName() { return repName; }

        public String getSpecimen() { return specimen; }

        public String getRegTime() { return regTime; }
    }
}
